home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech's Sprocket™ / SprocketGX / ExperimentalStuff / MailableWindow.cp < prev    next >
Encoding:
Text File  |  1994-10-17  |  6.0 KB  |  244 lines  |  [TEXT/MMCC]

  1. /*
  2.     File:        MailableWindow.cp
  3.  
  4.     Contains:    A AOCE-aware window base class
  5.                 
  6.     Written by: Dave Falkenburg with help from Steve Falkenburg’s
  7.                 CollaboDraw “object oriented C” sample application.
  8.     
  9.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     
  13.          <2>     8/26/94    DRF        Added AdjustPerfectWindowSizeForMailer.
  14.     To Do:        Just about everything
  15.     
  16.  */
  17.  
  18. #include <Types.h>
  19. #include <OCEStandardMail.h>
  20.  
  21. #include "AppLib.h"
  22. #include "MailableWindow.h"
  23.  
  24.  
  25. TMailableWindow::TMailableWindow()
  26.  : fMailerIsAttached(false),
  27.     fMailerIsExpanded(false)
  28. {
  29.     //    Remember, we can’t do anything in here because of C++’s insistance
  30.     //    of creating objects from the bottom-up.  If we call CreateWindow in
  31.     //    here then we end up calling the pure-virtual version of MakeNewWindow.
  32.  
  33. }
  34.  
  35. TMailableWindow::~TMailableWindow()
  36. {
  37. #if    qDebug
  38.     //    Release any PowerTalk things if they are still around
  39.  
  40.     if (fMailerIsAttached)
  41.         DebugStr("\pDidn’t call TMailableWindow::Close to dispose mailer");
  42. #endif
  43. }
  44.  
  45. Boolean TMailableWindow::EventFilter(EventRecord * anEvent)
  46. {
  47.     if (gHasAOCE)
  48.     {
  49.         SMPMailerResult    whatHappened;
  50.         SMPMailerState    mailerState;
  51.         
  52.         (void) SMPMailerEvent(anEvent,&whatHappened,FrontWindowProcForAOCEUPP,0);
  53.         (void) SMPGetMailerState(fWindow,&mailerState);
  54.  
  55.         //    ALOT MORE STUFF GOES IN HERE!
  56.  
  57.         // track if the mailer has been expanded or contracted        
  58.         if ((whatHappened & kSMPContractedMask) != 0)
  59.             this->ExpandOrContractMailer(false);
  60.         else if ((whatHappened & kSMPExpandedMask) != 0)
  61.             this->ExpandOrContractMailer(true);
  62.  
  63.         // check to see if the app must handle this event
  64.         if ((whatHappened & kSMPAppMustHandleEventMask) != 0)
  65.             return false;
  66.         else
  67.             return true;    //    nope, PowerTalk dealt with the event, we don’t have to.
  68.     }
  69.  
  70.     return false;
  71. }
  72.  
  73. void TMailableWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  74. {
  75. #if    qDebug
  76.     //    You can’t put a mailer in a floating window or a modal window!
  77.  
  78.     if (typeOfWindowToCreate != kNormalWindow)
  79.         DebugStr("\pTMailableWindow: Must be normal windows");
  80. #endif
  81.  
  82.     TWindow::CreateWindow();
  83.     
  84.     if (gHasAOCE && gPreferences.fMailPreferences.fCreateMailerForNewDocuments)
  85.         this->AttachMailerToWindow(true);
  86.  
  87.     fContentRect = fWindow->portRect;
  88. }
  89.  
  90. Boolean TMailableWindow::Close(void)
  91. {
  92.     Boolean    reallyClosing = true;
  93.     
  94.     if (fMailerIsAttached)
  95.     {
  96.         //    Offer to send or save the letter if it has changed
  97.  
  98.     }
  99.     else
  100.     {
  101.         //    Offer to save the document if it is dirty
  102.  
  103.     }
  104.  
  105.     if (fMailerIsAttached)
  106.     {
  107.         //    We need to remove the mailer before calling TWindow::Close
  108.         //    otherwise AOCE will get very confused. Too bad they didn’t
  109.         //    integrate better with the window manager to catch this for us.
  110.         
  111.         this->RemoveMailerFromWindow();
  112.     }
  113.  
  114.     if (reallyClosing)
  115.         return TWindow::Close();
  116.  
  117.     return false;
  118. }
  119.  
  120. OSErr TMailableWindow::AttachMailerToWindow(Boolean createExpanded)
  121. {
  122.     Point    where = {0,0};
  123.     OSErr    err;
  124.         
  125.     err = SMPNewMailer(fWindow,where,kCanContract,createExpanded,kDefaultAuthIdentity,nil,0L);
  126.     fMailerIsAttached = (err == noErr);
  127.     fMailerIsExpanded = kInitiallyExpanded;
  128.     
  129.     return err;    
  130. }
  131.  
  132. OSErr TMailableWindow::RemoveMailerFromWindow(void)
  133. {
  134.     OSErr                err = noErr;
  135.     SMPCloseOptions        closeOptions;
  136.     
  137.     closeOptions.moveToTrash = false;
  138.     closeOptions.addTag = false;
  139.     closeOptions.tag.dataLength = 0;
  140.     
  141.     if (fMailerIsAttached)
  142.     {
  143.         //    Get rid of the PowerTalk mailer header    
  144.         err = SMPDisposeMailer(fWindow,&closeOptions);
  145.         
  146.         fMailerIsAttached = false;
  147.         fContentRect = fWindow->portRect;
  148.     }
  149.         
  150.     return err;
  151. }
  152.  
  153. void TMailableWindow::Draw(void)
  154. {
  155.     //    Because of SMPMailerEvent, the PowerTalk mailer, if present will be drawn by AOCE
  156.     //    This means the window’s draw method only needs to worry drawing the other content
  157.  
  158.     RgnHandle    originalClipRgn = NewRgn();
  159.     
  160.     GetClip(originalClipRgn);
  161.     ClipRect(&fContentRect);
  162.  
  163.     //    Possible optimization by checking content rect against visRgn
  164.     //    « we probably also want to do a wacky SetOrigin call here »    
  165.     this->DrawContents();
  166.  
  167.     SetClip(originalClipRgn);
  168.     DisposeRgn(originalClipRgn);
  169. }
  170.  
  171. void TMailableWindow::AdjustPerfectWindowSizeForMailer(Rect * perfectSize)
  172. {
  173.     //    If a mailer is attached to the window, figure out how big it is
  174.     //    and make sure that the window is wide enough to deal with it.
  175.  
  176.     if (fMailerIsAttached)
  177.     {
  178.         short    expHeight,contHeight,mWidth;
  179.     
  180.         (void) SMPGetDimensions(&mWidth,&contHeight,&expHeight);    
  181.     
  182.         //    Make sure the perfect size is wide enough for the standard mailer.
  183.     
  184.         if (perfectSize->right < perfectSize->left + mWidth)
  185.             perfectSize->right = perfectSize->left + mWidth;
  186.  
  187.         //    We might also want to adjust height, but for now ignore the problem
  188.     }
  189. }
  190.     
  191. void TMailableWindow::AdjustForNewWindowSize(Rect * /* oldRect */,Rect * newRect)
  192. {
  193.     //    NOTE: Assumes rect is always zero-based
  194.  
  195.     fContentRect.bottom = newRect->bottom;
  196.     fContentRect.right = newRect->right;
  197. }
  198.  
  199. void TMailableWindow::ExpandOrContractMailer(Boolean doExpand)
  200. {
  201.     CSavePort  aSavePort(fWindow);
  202.     Rect    prevContentRect, newContentRect;
  203.     short    expHeight,contHeight,mWidth;
  204.  
  205.     (void) SMPGetDimensions(&mWidth,&contHeight,&expHeight);    
  206.  
  207.     prevContentRect = fContentRect;
  208.     newContentRect = fWindow->portRect;
  209.  
  210.     if (doExpand)
  211.         newContentRect.top += expHeight;
  212.     else
  213.         newContentRect.top += contHeight;
  214.  
  215.     this->AdjustForNewContentRect(&prevContentRect,&newContentRect);
  216.     fContentRect = newContentRect;
  217.  
  218.     (void) SMPExpandOrContract(fWindow,doExpand);
  219.     fMailerIsExpanded = true;
  220.  
  221. }
  222.  
  223. void TMailableWindow::AdjustForNewContentRect(Rect * /* prevContentRect */,Rect * newContentRect)
  224. {
  225.     //    the default thing to do is to force a complete redraw of the window’s normal
  226.     //    content area. We could be clever and use scrollrect to keep the bits around,
  227.     //    but for now we cheeze out.
  228.     
  229.     InvalRect(newContentRect);
  230. }
  231.  
  232. void TMailableWindow::DrawContents(void)
  233. {
  234. }
  235.  
  236. //    FrontWindow custom procedure for AOCE standard mail package:
  237. //        It enables some of AOCE to cleanly interoperate with “Dean Yu”-style floating windows
  238. pascal    WindowPtr FrontWindowProcForAOCE(long /* unusedParam */)
  239. {
  240.     return FrontNonFloatingWindow();
  241. }
  242.  
  243. FrontWindowUPP FrontWindowProcForAOCEUPP = NewFrontWindowProc(&FrontWindowProcForAOCE);
  244.